//
//  MovieListViewController.m
//  UI16.真.豆瓣
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 Amydom. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListCell.h"
#import "AppTools.h"
#import "Movie.h"
#import "DetailViewController.h"
#import "MBProgressHUD.h"
#import "SudokuMovieLIstViewController.h"
@interface MovieListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UITableView *myTabbleView;

@property (nonatomic ,retain)NSMutableArray *movieArray;

@property (nonatomic ,retain)AppTools *appTools;
@property (nonatomic ,retain)MBProgressHUD *hub;


@end

@implementation MovieListViewController

-(void)dealloc{
    
    [_hub release];
    [_movieArray release];
    [_myTabbleView release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationItem.rightBarButtonItem  = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    
    
    
    self.myTabbleView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.myTabbleView.delegate = self;
    self.myTabbleView.dataSource = self;
    [self.view addSubview:self.myTabbleView];
    [_myTabbleView release];
    [self.myTabbleView registerClass:[MovieListCell class] forCellReuseIdentifier:@"cell"];
    self.movieArray = [NSMutableArray array];
    self.navigationItem.title = @"电影";
                       
    
    
    //请求数据
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";

#pragma mark 让菊花旋转起来
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载中....";
    
   
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *MovieFilePath = [document stringByAppendingPathComponent:@"movie.txt"];
    //先读本地
    self.movieArray = [NSKeyedUnarchiver unarchiveObjectWithFile:MovieFilePath];
    
    
    [AppTools getDataWithURL:urlStr Block:^(id result) {
//        NSLog(@"%@",result);
        NSMutableDictionary *dic = result;
        self.movieArray = [NSMutableArray array];
//        NSString *error_code = [dic objectForKey:@"error_code"];
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        if ([reason isEqualToString:@"success"]) {
            [self.movieArray removeAllObjects];
            for (NSMutableDictionary *dic in array) {
                Movie *movie = [[Movie alloc]init];
                [movie setValuesForKeysWithDictionary:dic];
                [self.movieArray addObject:movie];
                [movie release];
                
               
            }
            //添加文件
            [NSKeyedArchiver archiveRootObject:self.movieArray toFile:MovieFilePath ];
            //刷新页面一定是在右数据之后刷新**************
            [self.myTabbleView reloadData];
# pragma mark　－ 菊花停止(在view刷新时)
            [self.hub hide:YES];
            
            //1.建工程,搭框架
            //2.铺页面,先用死数据
            //3.请求数据
            //4.给view赋值数据
        }else{
            
          
            
            NSLog(@"失败");
            
        }
        
    }];
   
}
- (void)rightAction:(UIBarButtonItem *)btn{
    
    SudokuMovieLIstViewController *sdMovie = [[SudokuMovieLIstViewController alloc]init];
    [self.navigationController pushViewController:sdMovie animated:YES];
    
    
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.movieArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"cell";
    MovieListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  Movie *mov = [self.movieArray objectAtIndex:indexPath.row];
   
    cell.myMovie = mov;
    
    return  cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  185;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    detailVC.movieID = movie.movieId;
    
    
    
    
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    [detailVC release];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
